package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysTicketTypeDao;
import com.gusha.model.InfoContent;
import com.gusha.model.SysClassify;
import com.gusha.model.SysTicket;
import com.gusha.model.SysTicketType;
import com.gusha.service.SysTicketTypeService;
import com.gusha.utils.Constants;
import com.gusha.utils.DateUtils;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysTicketType")
public class SysTicketTypeController {
	@Autowired
	private SysTicketTypeDao sysTicketTypeDao;
	@Autowired
	private SysTicketTypeService sysTicketTypeService;
	
	@RequestMapping("list")
	@SysOperateLog(operate = "查看资讯列表")
	public String list(Model model, HttpServletRequest request) {

	    //请求参数
	    String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
	    String scenicName = WebValueUtil.getWebValue(request, "scenicName");
	    String outTicket = WebValueUtil.getWebValue(request, "outTicket");
	    //参数封装
	    int pageNo = 0;
	    if (StringUtils.isNotBlank(pageIndex)) {
	        pageNo = Integer.parseInt(pageIndex);
	    }
	    SysTicketType sysTicketType = new SysTicketType();
	    if(StringUtils.isNotBlank(outTicket)) {
	    	sysTicketType.setOutTicket(new BigInteger(outTicket));
	    }
	    if(StringUtils.isNotBlank(scenicName)) {
	    	sysTicketType.setScenicName(scenicName);
	    }
	    //获取分页数据
	    Sort sort = new Sort(Sort.Direction.DESC, "addTime");
	    Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
	    Page<SysTicketType> page = sysTicketTypeService.findAll(pageable, sysTicketType);
	    model.addAttribute("page", page);
	    return "/admin/sysTicketType/list";
	}
	
	@RequestMapping("toAdd")
	@SysOperateLog(operate = "进入门票添加页面")
	public String toAdd(Model model) {
	
	
	    return "/admin/sysTicketType/form";
	}
	
	@RequestMapping("toEdit")
    @SysOperateLog(operate = "进入资讯编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(sysTicketTypeDao.existsById(new BigInteger(id))) {
                SysTicketType sysTicketType = sysTicketTypeDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysTicketType);
            }
        }        
      
        return "/admin/sysTicketType/form";
    }
	
	@RequestMapping("save")
	@ResponseBody
	@SysOperateLog(operate = "保存资讯信息")
	public JsonResult save(SysTicketType sysTicketType,HttpServletRequest request) {
	    JsonResult result = new JsonResult(false,"保存失败");
	    String infoIconOld = WebValueUtil.getWebValue(request, "infoIconOld");
        if(!infoIconOld.equals(sysTicketType.getTicketImg())) {
        	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
             MultipartFile file = multipartRequest.getFile("file");
          
              // 要上传的目标文件存放路径
             String newFileName = FileUtils.getFileName(file.getOriginalFilename());
        
            // String newFileName = file.getOriginalFilename();
           String imagePath = Constants.getFilePath("ticketType")+newFileName;
             String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("ticketType");
            // String localPath = Constants.IMAGE_LOCAL_PATH;
             FileUtils.upload(file, localPath, newFileName);
             sysTicketType.setTicketImg(imagePath);
        }
	    sysTicketTypeDao.saveAndFlush(sysTicketType);
	    result.setFlag(true);
	    result.setMsg("保存成功");
	    return result;
	}
	
	  // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysTicketTypeDao.existsById(id)) {
            SysTicketType sysTicketType = sysTicketTypeDao.getOne(id);
            sysTicketType.setIsDelete(new BigInteger("1") );
            sysTicketTypeDao.saveAndFlush(sysTicketType);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
    
    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复资讯信息")
    public JsonResult recovery(BigInteger id) {
    	  JsonResult result = new JsonResult(false,"删除失败");
          if (sysTicketTypeDao.existsById(id)) {
              SysTicketType sysTicketType = sysTicketTypeDao.getOne(id);
              sysTicketType.setIsDelete(new BigInteger("0") );
              sysTicketTypeDao.saveAndFlush(sysTicketType);
              result.setFlag(true);
              result.setMsg("删除成功");
          }else{
              result.setFlag(false);
              result.setMsg("该对象信息不存在");
              return result;
          }
          return result;
    }


}
